package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 电子技术设计
 * @author Persh
 *
 */
public class EdnchinaNewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://www.ednchina.com/ART_[0-9]*_[0-9]*_[0-9]*_[A-Z]*_[a-z0-9]*.HTM",
		"http://www.ednchina.com/articleLogin.do\\?artId=[0-9]*&fromWhere=.*"
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();	
		String author="/";
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div#content_zoom"));
		String dateStr = doc.select("div.article_function").text();
		if(StringUtils.isBlank(dateStr)){
			dateStr = doc.select("span a").text();
		}
		date=DateUtils.matchDate(dateStr);

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		news.setAuthor(author);
		
		return news;
	}
	
	public static void main(String[] args) {
		EdnchinaNewsAnalyse analyse=new EdnchinaNewsAnalyse();
		String url="http://archive.ednchina.com/www.ednchina.com/ART_8800525515_16_20037_OT_de607f1e.HTM";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
